package com.bcinfo.iccard.busi.handler;

import org.apache.commons.lang3.exception.ExceptionUtils;

import com.bcinfo.iccard.busi.AbstractBusinessHandler;
import com.bcinfo.iccard.enums.Constants;
import com.bcinfo.iccard.exception.IccardException;
import com.bcinfo.iccard.model.Pos;
import com.bcinfo.iccard.model.RechargeableCard;
import com.solab.iso8583.IsoMessage;
import com.solab.iso8583.IsoType;
/**
 * 余额查询
 * @author wangguangchun
 *
 */
public class BalanceQueryHandler extends AbstractBusinessHandler {

	@Override
	public void handleBusiness(IsoMessage req, IsoMessage resp)
			throws Exception {
	    if(req==null ||resp==null){
            throw new IccardException("iso message req or resp is null,discard this request!");
        }
	    if(_logger.isDebugEnabled()){
	        _logger.debug("handleBusiness:"+req.toString());
	    }
	    //相同的域进行copy
	    resp.copyFieldsFrom(req, 3,11,24,41,42);
	    resp.setValue(54,0, IsoType.LLVAR, 0);
	    try
        {
	        RechargeableCard card=checkCardNo(req);
	        if(card==null){
	            resp.setValue(39,Constants.ResponeCode.invalidCardNum.value,IsoType.ALPHA,2);
	        }else{
	            Pos pos=getPos(req);
	            String tradeType =(String)req.getField(22).getValue();
	            boolean pinOk=false;
	            if(Constants.TradeType.cardWithPwd.value.equals(tradeType)||Constants.TradeType.manualWithPwd.value.equals(tradeType)){
	                String pinKey=pos.getPik();
	                byte[] pinBlockFromClient=(byte[])req.getField(52).getValue();
	                pinOk=checkPin(pinBlockFromClient, pinKey, card);
	                if(pinOk){
	                    setBalance(resp, card);
	                }else{
	                    resp.setValue(39,Constants.ResponeCode.incorrectPin.value,IsoType.ALPHA,2);
	                }
	            }else{
	                if(_logger.isDebugEnabled()){
	                    _logger.debug("skip check pin with tradeType "+tradeType);
	                }
	                setBalance(resp, card);
	            }
	        }
        }
        catch (Exception e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            resp.setValue(39,Constants.ResponeCode.timeOut.value,IsoType.ALPHA,2);
        }
	}

    /**
     * 一句话描述 <br/>
     * <p>Description: TODO(详细描述) 
     * <br/>
     * <p>Author: wangguangchun<br/>
     * <p>Date: 2013-12-26-下午01:55:51<br/>
     * <p>
     * @param resp
     * @param card   
     * 
     */
    private void setBalance(IsoMessage resp, RechargeableCard card)
    {
        float balance=card.getRechargeableCardAccountTotal();
        long fen=(long)(balance*100);
        //索引参考号
        resp.setValue(37,String.valueOf(System.currentTimeMillis()).substring(1),IsoType.ALPHA,12);
        //余额
        resp.setValue(54,wrapBalance(fen), IsoType.LLVAR, 0);
        //自定义域
        resp.setValue(39,Constants.ResponeCode.approval.value,IsoType.ALPHA,2);
    }

	/* (non-Javadoc)
	 * Description: <br/> 
	 *
	 * @param req
	 * @param macKey
	 * @return 
	 * @see com.bcinfo.iccard.busi.AbstractBusinessHandler#authenticate(com.solab.iso8583.IsoMessage, java.lang.String)
	 */
	@Override
	protected boolean authenticate(IsoMessage req, String macKey)
	{
	    // 测试。不校验mac
	    return true;
	}
}
